0001 // 0002 // BigUInt Addition.swift 0003 // BigInt 0004 // 0005 // Created by Károly Lőrentey on 2016-01-03. 0006 // Copyright © 2016 Károly Lőrentey. All rights reserved. 0007 // 0008 0009 import Foundation 0010 0011 extension BigUInt { 0012 //MARK: Addition 0013 0014 /// Add the digit `d` to this integer in place. 0015 /// `d` is shifted `shift` digits to the left before being added. 0016 /// 0017 /// - Complexity: O(max(count, shift)) 0018 public mutating func addDigitInPlace(d: Digit, shift: Int = 0) { 0019 precondition(shift >= 0) 0020 lift() 0021 var carry: Digit = d 0022 var i = shift 0023 while carry > 0 { 0024 let (d, c) = Digit.addWithOverflow(self[i], carry) 0025 self[i] = d 0026 carry = (c ? 1 : 0) 0027 i += 1 0028 } 0029 } 0030 0031 /// Add the digit `d` to this integer and return the result. 0032 /// `d` is shifted `shift` digits to the left before being added. 0033 /// 0034 /// - Complexity: O(max(count, shift)) 0035 @warn_unused_result 0036 public func addDigit(d: Digit, shift: Int = 0) -> BigUInt { 0037 var r = self 0038 r.addDigitInPlace(d, shift: shift) 0039 return r 0040 } 0041 0042 /// Add `b` to this integer in place. 0043 /// `b` is shifted `shift` digits to the left before being added. 0044 /// 0045 /// - Complexity: O(max(count, b.count + shift)) 0046 public mutating func addInPlace
BigUInt Addition.swift:38 r.addDigitInPlace(d, shift: shift)BigUInt Addition.swift:83 self.addDigitInPlace(1, shift: shift)BigUInt Radix Conversion.swift:71 self.addDigitInPlace(d)(b: BigUInt, shift: Int = 0) { 0047 precondition(shift >= 0) 0048 lift() 0049 var carry = false 0050 var bi = 0 0051 while bi < b.count || carry { 0052 let ai = shift + bi 0053 let (d, c) = Digit.addWithOverflow(self[ai], b[bi]) 0054 if carry { 0055 let (d2, c2) = Digit.addWithOverflow(d, 1) 0056 self[ai] = d2 0057 carry = c || c2 0058 } 0059 else { 0060 self[ai] = d 0061 carry = c 0062 } 0063 bi += 1 0064 } 0065 } 0066 0067 /// Add `b` to this integer and return the result. 0068 /// `b` is shifted `shift` digits to the left before being added. 0069 /// 0070 /// - Complexity: O(max(count, b.count + shift)) 0071 @warn_unused_result 0072 public func add
BigUInt Addition.swift:74 r.addInPlace(b, shift: shift)BigUInt Addition.swift:101 a.addInPlace(b, shift: 0)BigUInt Multiplication.swift:52 guard y != 1 else { self.addInPlace(x, shift: shift); return }BigUInt Multiplication.swift:123 r.addInPlace(xh * y, shift: x.middleIndex)BigUInt Multiplication.swift:129 r.addInPlace(yh * x, shift: y.middleIndex)BigUInt Multiplication.swift:149 r.addInPlace(high, shift: 2 * shift)BigUInt Multiplication.swift:150 r.addInPlace(low, shift: shift)BigUInt Multiplication.swift:151 r.addInPlace(high, shift: shift)BigUInt Multiplication.swift:156 r.addInPlace(m, shift: shift)(b: BigUInt, shift: Int = 0) -> BigUInt { 0073 var r = self 0074 r.addInPlace(b, shift: shift) 0075 return r 0076 } 0077 0078 /// Increment this integer by one. If `shift` is non-zero, it selects 0079 /// the digit that is to be incremented. 0080 /// 0081 /// - Complexity: O(count + shift) 0082 public mutating func increment(shift shift: Int = 0) { 0083 self.addDigitInPlace(1, shift: shift) 0084 } 0085 } 0086 0087 //MARK: Addition 0088 0089 /// Add `a` and `b` together and return the result. 0090 /// 0091 /// - Complexity: O(max(a.count, b.count)) 0092 @warn_unused_result 0093 public func +(a: BigUInt, b: BigUInt) -> BigUInt { 0094 return a.add(b) 0095 } 0096 0097 /// Add `a` and `b` together, and store the sum in `a`. 0098 /// 0099 /// - Complexity: O(max(a.count, b.count)) 0100 public func +=(inout a: BigUInt, b: BigUInt) { 0101 a.addInPlace(b, shift: 0) 0102 } 0103 0104 0105
BigUInt Addition.swift:94 return a.add(b)